//
//  MeiYinBiViewC.m
//  BeautyPicture
//
//  Created by LiuNa on 16/3/9.
//  Copyright © 2016年 szb. All rights reserved.
//

#import "MeiYinBiViewC.h"
#import "MeiYinTableViewCell.h"
#import "BPhttpTool.h"
#import "AccountTool.h"
#import "MBProgressHUD+MJ.h"
#import "BPLoginViewController.h"

@interface MeiYinBiViewC ()<UITableViewDataSource, UITableViewDelegate>
{
    MBProgressHUD *_postHud;
}


@property (nonatomic, strong) UITableView *tabel;

// 所有的数据
@property (nonatomic, strong) NSArray *arrAll;

// list 里面的数据
@property (nonatomic, strong) NSDictionary *dicAll;

@property (nonatomic, strong) NSDictionary *listDic;

// 美印币的 剩余
@property (nonatomic, strong) NSString *score;

//美印币的 总数
@property (nonatomic, strong) NSString *total;

@end

@implementation MeiYinBiViewC


- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [self Createdata];
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"美印币";
    [self.view.subviews[0] removeFromSuperview];
    
    self.arrAll = [NSArray array];
    self.dicAll = [NSDictionary dictionary];
    self.listDic = [NSDictionary dictionary];
    
    
    
    
    // Do any additional setup after loading the view.
}


- (void)create{
    
    CGFloat headH = 200.0f;
    // 头部的 UIView
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, headH)];
    headView.backgroundColor = BPMeiYinBiColor;
    [self.view addSubview:headView];
    
    CGFloat backX = 15.0f;
    CGFloat backY = 22.0f;
    CGFloat backW = 25.0f;
    CGFloat backH = 25.0f;
    // 返回按钮
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(backX, backY + 3, backW, backH)];
    [back setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:back];
    
    // 头像
    UIImageView *imageHead = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, backY + 5.0f, backW + 85.0f, backH + 70.0f)];
    imageHead.image = [UIImage imageNamed:@"meiYinBi"];
    [headView addSubview:imageHead];
    
    
    CGFloat labY = 130;
    // 左边的 label
    UILabel *labelLeftNum = [[UILabel alloc] initWithFrame:CGRectMake(backX - 10, labY, labY, 40)];
    labelLeftNum.text = self.total;
    labelLeftNum.textColor = [UIColor whiteColor];
    labelLeftNum.font = [UIFont systemFontOfSize:30];
    labelLeftNum.textAlignment = 1;
    [headView addSubview:labelLeftNum];
    
    UILabel *labelLeftText = [[UILabel alloc] initWithFrame:CGRectMake(backX - 12, labY + 30, 150, 30)];
    labelLeftText.text = @"美印币累计邀请效益(个)";
    labelLeftText.textColor = [UIColor whiteColor];
    labelLeftText.font = [UIFont systemFontOfSize:10];
    labelLeftText.textAlignment = 1;
    [headView addSubview:labelLeftText];
    
    
    // 右边的 label
    UILabel *labelRightNum = [[UILabel alloc] initWithFrame:CGRectMake(labY + 50, labY, 130, 40)];
    labelRightNum.text = self.score;
    labelRightNum.textColor = [UIColor whiteColor];
    labelRightNum.font = [UIFont systemFontOfSize:30];
    labelRightNum.textAlignment = 1;
    [headView addSubview:labelRightNum];
    
    UILabel *labelRightText = [[UILabel alloc] initWithFrame:CGRectMake(labY + 40, labY + 30, 150, 30)];
    labelRightText.text = @"剩余美印币(个)";
    labelRightText.textColor = [UIColor whiteColor];
    labelRightText.font = [UIFont systemFontOfSize:10];
    labelRightText.textAlignment = 1;
    [headView addSubview:labelRightText];
    
    
    // 日期
    UILabel *day = [[UILabel alloc] initWithFrame:CGRectMake(10, headH, 50, 30)];
    day.text = @"日期";
    day.textColor = [UIColor blackColor];
    [self.view addSubview:day];
    
    // 内容
    UILabel *nR = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25, headH, 50, 30)];
    nR.text = @"内容";
    nR.textColor = [UIColor blackColor];
    [self.view addSubview:nR];
    
    // 数量
    UILabel *num = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, headH, 50, 30)];
    num.text = @"数量";
    num.textColor = [UIColor blackColor];
    [self.view addSubview:num];
    
    
    // 波纹线
    UIImageView *bowen = [[UIImageView alloc] initWithFrame:CGRectMake(0, headH + 30, self.view.frame.size.width, 5)];
    bowen.image = [UIImage imageNamed:@"波纹线"];
    [headView addSubview:bowen];
}



- (void)Createdata{
    
//    if ([AccountTool account].uid == nil) {
//       
//        [MBProgressHUD showError:@"您还没有登录!"];
//        BPLoginViewController *logo = [[BPLoginViewController alloc] init];
//        logo.block = ^(){
//            NSLog(@"ggg");
//        };
//        [self.navigationController pushViewController:logo animated:YES];
//        
//    }else{
    
        _postHud = [MBProgressHUD showMessage:@"正在加载数据"];
        [BPhttpTool get:[NSString stringWithFormat:@"%@user/myscore/uid/%@", BPhttp, [AccountTool account].uid] params:nil success:^(id responseObj) {
            
            self.dicAll = responseObj;
            self.arrAll = [self.dicAll objectForKey:@"list"];
            
            self.score = [self.dicAll objectForKey:@"score"];
            self.total = [self.dicAll objectForKey:@"total"];
            
            if ([self.arrAll isEqual:@""]) {
                [self.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:@"您还没有积分"];
            }
            else{
                
                [MBProgressHUD hideHUD];
                [self create];
                [self createTabel];
            }
            
            
        } failure:^(NSError *error) {
            
        }];
//    }
}


- (void)createTabel{
    self.tabel = [[UITableView alloc] initWithFrame:CGRectMake(0, 240, self.view.frame.size.width, self.view.frame.size.height - 240) style:UITableViewStylePlain];
    self.tabel.delegate = self;
    self.tabel.dataSource = self;
    [self.view addSubview:self.tabel];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrAll.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeiYinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"meiyin"];
    if (!cell) {
        cell = [[MeiYinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"meiyin"];
    }
    
    self.listDic = [self.arrAll objectAtIndex:indexPath.row];
    
    cell.Day.text = [self.listDic objectForKey:@"create_time"];
    cell.neiRong.text = [self.listDic objectForKey:@"detail"];
    cell.number.text = [self.listDic objectForKey:@"score"];
    return cell;
}



// 返回按钮
- (void)back:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
